package com.itheimamobileguard.activitys;


import com.example.mobile.R;
import com.itheimamobileguard.utils.Md5Utils;
import com.itheimamobileguard.utils.UIUtils;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeActivity extends Activity {
	
	//配置信息
	private  SharedPreferences sp;
	private GridView gv_home;
	private String[] name = {"手机防盗","通讯卫士","软件管家","进程管理","流量统计","手机杀毒","缓存清理","高级工具","设置中心"};
	private int[] icons = {R.drawable.safe,R.drawable.callmsgsafe,
			R.drawable.app_selector,R.drawable.taskmanager,R.drawable.netmanager,
			R.drawable.trojan,R.drawable.sysoptimize,R.drawable.atools,
			R.drawable.settings};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		gv_home = (GridView) findViewById(R.id.gv_home);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		//设置gridview条目的点击事件
		gv_home.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent;
				switch (position) {
				case 0:
					if (isSetUpPwd()) {
						//设置过密码，显示输入密码对话框
						showEnterPwdDialog();
					}else{
						//没有设置过密码，显示设置秘密对话框
						showSetupPwdDialog();
					}
					break;
				case 1:	//通讯卫视
					intent = new Intent(HomeActivity.this,CallSmsSafeActivity.class);
					startActivity(intent);
					break;
				case 8:	//设置中心
					intent = new Intent(HomeActivity.this,SettingCenterActivity.class);
					startActivity(intent);
					break;
				}
			}
		});
	}
	
	@Override
	protected void onStart() {
		gv_home.setAdapter(new HomeAdapter());
		super.onStart();
	}
	
	private EditText et_pwd;
	private EditText et_pwd_confirm;
	private Button set_bt_ok;
	private Button set_bt_cancel;
	private AlertDialog alertDialog;
	
	/**
	 * 显示设置密码对话框
	 */
	protected void showSetupPwdDialog() {
		AlertDialog.Builder builde = new Builder(this);
		View view = View.inflate(this, R.layout.dialog_setup_pwd, null);
		et_pwd = (EditText) view.findViewById(R.id.et_pwd);
		et_pwd_confirm = (EditText) view.findViewById(R.id.et_pwd_confirm);
		set_bt_ok = (Button) view.findViewById(R.id.setup_bt_ok);
		set_bt_cancel = (Button) view.findViewById(R.id.setup_bt_cancl);
		
		set_bt_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDialog.dismiss();
			}
		});
		
		set_bt_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String pwd = et_pwd.getText().toString().trim();
				String pwd_confirm = et_pwd_confirm.getText().toString().trim();
				if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd_confirm)) {
					UIUtils.showToast(HomeActivity.this, "密码不能为空");
					return;
				}
				if (!pwd.equals(pwd_confirm)) {
					UIUtils.showToast(HomeActivity.this, "两次密码不一致");
					return;
				}
				Editor editor = sp.edit();
				editor.putString("password", Md5Utils.encode(pwd));
				editor.commit();
				alertDialog.dismiss();
			}
		});
		builde.setView(view);
		alertDialog = builde.show();
	}
	
	
	private EditText et_enter;
	private Button btn_ok;
	private Button btn_cancle;
	/**
	 * 显示输入密码对话框
	 */
	protected void showEnterPwdDialog() {
		AlertDialog.Builder builde = new Builder(this);
		View view = View.inflate(this, R.layout.dialog_enter_pwd, null);
		et_enter = (EditText) view.findViewById(R.id.et_enter);
		btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
		btn_ok = (Button) view.findViewById(R.id.btn_ok);
		
		btn_cancle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDialog.dismiss();
			}
		});
		
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String pwd = et_enter.getText().toString().trim();
				if (TextUtils.isEmpty(pwd)) {
					UIUtils.showToast(HomeActivity.this, "密码不能为空");
					return;
				}
				
				String pwd_set = sp.getString("password", "");
				if (Md5Utils.encode(pwd).equals(pwd_set)) {
					Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
					startActivity(intent);
					alertDialog.dismiss();
					
				}else{
					UIUtils.showToast(HomeActivity.this, "密码错误，请重新输入");
				}
			}
		});
		
		builde.setView(view);	
		alertDialog = builde.show();
	}

	/**
	 * 判断用户是否设置过密码
	 * @return
	 */
	private boolean isSetUpPwd(){
		String password = sp.getString("password", null);
		if (TextUtils.isEmpty(password)) {
			return false;
		}else{
			return true;
		}
	}
	
	private class HomeAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return name.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			if (convertView == null) {
				view = View.inflate(getApplicationContext(), R.layout.item_home_grid, null);
			}else{
				view = convertView;
			}
			ImageView iv = (ImageView) view.findViewById(R.id.iv_homeitem_icon);
			TextView tv = (TextView) view.findViewById(R.id.tv_homeitem_name);
			tv.setText(name[position]);
			if (position == 0) {
				String newName = getSharedPreferences("config",MODE_PRIVATE).getString("newName1", "");
				if (!TextUtils.isEmpty(newName)) {
					tv.setText(newName);
				}
			}
			iv.setImageResource(icons[position]);
			return view;
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

	}
}
